﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using System.Drawing;
using Silvermoon.OpenGL;
using Silvermoon.OpenGL.Native;
using Silvermoon.UI;
using Silvermoon.Images;
using Silvermoon.Utils;
using Silvermoon.Shapes;
using System.IO;
using System.Threading;
using Silvermoon.Animations;
using Silvermoon.Windows;

namespace Testing
{
    /// <summary>
    /// Panorama View demo.
    /// </summary>
    public class HubDemo : DemoWindowBase
    {
        public static bool blend = false;

        const int itemTileSize = 180;
        const int leftEdge = 0;
        private Panorama hub;
        private PanoramaSection sect1, sect2, sect3;

        private static Texture panoramaImage;
        private static FontSprite itemFont;

        public class ImageItem : Item
        {
            protected override Silvermoon.Shapes.Shape CreateDefaultBackground()
            {
                //return new TileShape { Color = PhoneColors.Phone7Blue, Opacity = 240 };
                return new RectangleShape { Color = PhoneColors.Blue, Opacity = 200 };
            }


            public ImageItem(Color color)
                : base()
            {
                this.Color = color;
            }

            protected override void Dispose(bool dispose)
            {
                base.Dispose(dispose);
            }

            protected override void OnLayout()
            {
                base.OnLayout();
                Background.Bounds = new Rectangle(0, 0, itemTileSize, itemTileSize);
            }
        }

        static HubDemo()
        {
            // create some static objects:
            //  backgroundImage = Texture.FromResource("Testing.Images.panoramic.jpg", TextureFormat.RGB565);

            itemFont = FontSprite.FromData(GlFontFamily.GenericSansSerif, 16, FontStyle.Regular, false);

        }


        protected override Shape CreateDefaultBackground()
        {
            return Color.Black;
        }

        public HubDemo()
            : base()
        {
            Color = Color.Black;
            SetTransition(500, TransitionMask.RotateLeftIn | TransitionMask.None, 600, TransitionMask.RotateLeftOut | TransitionMask.None);
            ToolbarOpacity = 0;

            Texture image = panoramaImage;
            hub = new Panorama
            {
                PanoramaImageOpacity = 200,
                Title = "Panorama View",
                Margin = Margin.Zero,
                PanoramaImage = image
            };
            Controls.Add(hub);

            sect1 = CreateSection1("gallery");
            sect2 = CreateSection2("sample pictures", 800);
            sect3 = CreateSection3("what's new");
            //sect4 = CreateSection3("Section 4");

            if (sect1 != null) hub.Sections.Add(sect1);
            if (sect2 != null) hub.Sections.Add(sect2);
            if (sect3 != null) hub.Sections.Add(sect3);

            // if the background image is not yet loaded, load it asynchronously in the background and get notified when it's loaded:
            if (image == null)
            {
                hub.PanoramaImageOpacity = 0;
                PanoramaImageLoader.Loader.Loaded += new EventHandler(OnImageLoaded);
            }
            else if (blend)
            {
                OnImageLoaded(null, EventArgs.Empty);
            }
        }

        /// <summary>
        /// When the background image is loaded, add it to the hub and let it fade in.
        /// </summary>
        void OnImageLoaded(object sender, EventArgs e)
        {
            panoramaImage = PanoramaImageLoader.Loader.Image;
            hub.PanoramaImage = panoramaImage;
            hub.PanoramaImageOpacity = 0;
            IntTransition transition = new IntTransition
            {
                Delay = sender == null ? 700 : 0,
                Property = Panorama.PanoramaImageOpacityProperty,
                Duration = 550,
                From = 0,
                To = 200,
                EasingFunction = LinearEase.Instance,
                Tracking = TrackMode.Keep
            };
            Panorama.PanoramaImageOpacityProperty.BeginAnimation(hub, transition);
        }

        private PanoramaSection CreateSection1(string title)
        {
            PanoramaSection section = new PanoramaSection
            {
                Margin = Margin.Zero
            };

            Label l = new Label
            {
                Text = title,
                Margin = new Margin(0, 0, 0, null),
                Height = 64,
                Alignment = Alignment.Near,
                LineAlignment = Alignment.Center,
                Font = FontSprite.Bold,
                FontScale = 1.5f
            };
            section.Controls.Add(l);

            ListView listBox = new ListView { Margin = new Margin(leftEdge, 70, 16, 2), ScrollbarColor = Color.Black };
            section.Controls.Add(listBox);


            listBox.Items.Add(new TextItem("albums", itemFont));
            listBox.Items.Add(new TextItem("all", itemFont));
            listBox.Items.Add(new TextItem("favorites", itemFont));
            listBox.TapItem += OnDialogButtonTap;

            return section;
        }

        private PanoramaSection CreateSection2(string title, int width)
        {
            PanoramaSection section = new PanoramaSection
            {
                Width = width
            };

            Label l = new Label
            {
                Text = title,
                Bounds = new Rectangle(leftEdge, 0, width - leftEdge, 64),
                Alignment = Alignment.Near,
                LineAlignment = Alignment.Center,
                Font = FontSprite.Bold,
                FontScale = 1.5f
            };
            section.Controls.Add(l);

            ListView listBox = new ListView
            {
                Margin = new Margin(leftEdge, 90, 0, 2)
            };
            ListViewGroup sec = new ListViewGroup
            {
                ItemsPerRow = 4,
                ItemHeight = itemTileSize + 16
            };

            List<ImageItem> items = new List<ImageItem>(8);
            for (int i = 0; i < 8; i++)
            {
                ImageItem item = new ImageItem(PhoneColors.Blue);
                items.Add(item);
            }
            sec.ItemsSource = items;
            listBox.Items.Add(sec);

            section.Controls.Add(listBox);
            return section;
        }

        private PanoramaSection CreateSection3(string title)
        {
            PanoramaSection section = new PanoramaSection
            {
                Margin = Margin.Zero
            };

            Label l = new Label
            {
                Text = title,
                Height = 64,
                Margin = new Margin(0, 0, 0, null),
                Alignment = Alignment.Near,
                LineAlignment = Alignment.Center,
                Font = FontSprite.Bold,
                FontScale = 1.5f,
                IsShadowed = true
            };
            section.Controls.Add(l);

            Label l2 = new Label
            {
                Text = "no news to share right now.",
                Margin = new Margin(0, 48, 0, null),
                Height = 64,
                TextColor = Color.Silver,
                IsShadowed = true
            };
            section.Controls.Add(l2);

            TextBox tb = new TextBox
            {
                Margin = new Margin(0, 200, 0, null),
                Height = 80,
            };
            section.Controls.Add(tb);

            return section;
        }

        void OnDialogButtonTap(object sender, ItemEventArgs e)
        {
            TextItem item = e.Item as TextItem;
            if (item != null)
            {
                Dialog dlg = new TextDialog(item.Text) { Height = 240 };
                dlg.ShowModal();
                HubDemo.blend ^= true;
            }
        }

        protected override void OnShowing()
        {
            base.OnShowing();
            AnimateHub();
        }

        private void AnimateHub()
        {
            IntTransition transition = new IntTransition
            {
                Delay = 250,
                Duration = 700,
                To = -600,
                Tracking = TrackMode.Keep
            };
            transition.Completed += new EventHandler(transition_Completed);
            Panorama.OffsetProperty.BeginAnimation(hub, transition);
        }

        void transition_Completed(object sender, EventArgs e)
        {
            Screen.Progress();
            PanoramaImageLoader.Loader.LoadImage();
        }

    }
}
